package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dao.EmployeeDao;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.exception.DuplicateUserException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;





@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public R<Employee> login(Employee employee) {
        //第一步验证用户名是否存在
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //select * from 表名 where username= #{username}
        lqw.eq(Employee::getUsername,employee.getUsername());
        //根据从网页上传过来的用户名查询数据
        Employee emp = employeeDao.selectOne(lqw);
        if (emp==null){
            return R.error("用户名不存在");
        }

        //用户名存在则对密码进行加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //判断用户名密码是否正确
        //用网页传递过来的密码,检验根据用户名在数据库查询到的面是否一致
        if (!emp.getPassword().equals(password)){
            return R.error("用户名密码错误");
        }
        //判断该用户是否被禁用
        if (emp.getStatus()==0){
            return R.error("此账户已被禁用");
        }
        return R.success(emp);
    }

    @Override
    public R<Employee> addEmp(Employee employee) {
        //设置密码并加密
        try {
            String password = DigestUtils.md5DigestAsHex("123456".getBytes());
            employee.setPassword(password);

            //创建时间修改时间
          /*  employee.setCreateTime(LocalDateTime.now());
            employee.setUpdateTime(LocalDateTime.now());*/

            //添加状态
            employee.setStatus(1);

            employeeDao.insert(employee);
        } catch (Exception e) {
            String message = e.getMessage();
            if (message.contains("Duplicate entry")){
                throw new DuplicateUserException("该用户已存在,请重新输入");
            }
        }

        return R.success(null);
        }


        //分页查询
    @Override
    public Page<Employee> findPage(int page, int pageSize, String name) {
        //mybatis分页查询类,参数为当前页码page,分页条数pageSize
        Page<Employee> p = new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> lwq = new LambdaQueryWrapper<>();
        /*StringUtils.isNotBlank(name) 等同于做了if判断
        if(name != null && name!="")

        用户上搜索了这个条件就会就会加上去,没有搜索就不会加上去
        */
        lwq.like(StringUtils.isNotBlank(name),Employee::getName,name);
        //调用分页查询方法返回一个Page对象
        Page<Employee> pages = employeeDao.selectPage(p, lwq);
        return pages;
    }

}




